package com.irdstudio.tdp.executor.core.plugin.git;

import com.irdstudio.tdp.executor.core.plugin.AbstractPlugin;
import com.irdstudio.sdk.beans.core.util.PropertiesUtil;
import org.eclipse.jgit.api.errors.GitAPIException;

import java.sql.Connection;

public class GitPushPlugin extends AbstractPlugin {

    private String pluginName = null;
    private PluginGitConf pluginParam = null;
    private String gitLocalPath=null;
    private String gitLocalPathFileName;
    /**
     * 更新到本地路径
     * @param localPath 本地仓库路径
     * @param remotePath 远程仓库地址
     * @param username 用户名
     * @param password 密码
     * @param branchName 分支
     * @return
     */
    public boolean repositoryPush(String localPath,String remotePath,String username,String password,String branchName){
        JGitUtil jGitUtil = new JGitUtil(localPath,remotePath,username,password);
        try {
            jGitUtil.addAll();
            jGitUtil.commit("auto code generate commit");
            jGitUtil.pull(branchName);
        } catch (Exception e) {
            this.logger.error("提交代码异常 " + e.getMessage(), e);
            return false;
        }
        return true;
    }

    @Override
    protected boolean doReadConfigureFromDB(Connection connection, String s) {
        pluginName = context.getSzPluginName();
        pluginParam = new PluginGitConf();
        pluginParam.setGitUserName(PropertiesUtil.getPropertyByKey("application", "git.username"));
        pluginParam.setGitPassword(PropertiesUtil.getPropertyByKey("application", "git.password"));
        pluginParam.setGitRemotePath(PropertiesUtil.getPropertyByKey("application", "git.remote.path"));
        pluginParam.setGitBranchName(PropertiesUtil.getPropertyByKey("application", "git.branch.name"));
        //获取文件目录名（如 /xxx)
        int firstIndex = pluginParam.getGitRemotePath().lastIndexOf("/");
        int lastIndex = pluginParam.getGitRemotePath().lastIndexOf(".");
        gitLocalPathFileName  = pluginParam.getGitRemotePath().substring(firstIndex, lastIndex);

        gitLocalPath = PropertiesUtil.getPropertyByKey("application", "git.local.path");
        if (pluginParam ==null || gitLocalPath ==null)
        {
            context.setSzLastErrorMsg("未读取到配置标识为：" + s + "的数据Git配置!");
            return false;
        }else {

            return true;
        }
    }

    @Override
    public boolean execute(){
        this.logger.info("........................................................................");
        boolean flag = true;

        if (gitLocalPath!=null){
            flag = repositoryPush(gitLocalPath+gitLocalPathFileName, pluginParam.getGitRemotePath(), pluginParam.getGitUserName(), pluginParam.getGitPassword(), pluginParam.getGitBranchName());
            if (flag){
                logger.info("代码更新成功，本地更新路径:"+ gitLocalPath+"\n远程路径为："+ pluginParam.getGitRemotePath());
            }
            else {
                logger.info("代码更新失败，本地更新路径:"+ gitLocalPath+"\n远程路径为："+ pluginParam.getGitRemotePath());
            }
            return flag;
        }else {
            return false;
        }

    }
}
